package com.monika.main.system.servcie.impl;

import com.monika.main.system.constant.CacheConstant;
import com.monika.main.system.dao.SysUserDao;
import com.monika.main.system.domain.entity.SysUser;
import com.monika.main.system.servcie.ISysUserService;
import com.monika.main.system.util.RedisCache;
import com.monika.main.system.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * Author whh
 * Date 2023/04/17/ 19:58
 * <p></p>
 */
@Service
public class SysUserServiceImpl implements ISysUserService {


    @Autowired
    private SysUserDao sysUserDao;

    @Override
    public SysUser querySysUserByUserName(String username) {
        return sysUserDao.querySysUserByUserName(username);
    }

    @Override
    public SysUser querySysUserCache(String username, int timeout, TimeUnit timeUnit) {

        if(StringUtil.isEmpty(username)){
            throw new IllegalArgumentException("username can not null or empty");
        }
        SysUser sysUser = RedisCache.get(CacheConstant.SYS_USER_KEY+username);

        if(Objects.isNull(sysUser)){
            sysUser = sysUserDao.querySysUserByUserName(username);
            RedisCache.put(CacheConstant.SYS_USER_KEY+username,sysUser,timeout,timeUnit);
        }
        return sysUser;
    }



    @Override
    public void updateUserProfile(SysUser sysUser) {

    }
}
